package training;

import hbase.classification.classification;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

import utils.mathCalc.Calculations;

public class zeroFrequencyProblem {
	private static Configuration config = HBaseConfiguration.create();
	/**
	 * Get all the word with less than 
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 */
	public static void train() throws IOException, ClassNotFoundException, SQLException{
		HTable frequencyAllEvent = new HTable(config, "frequencyAllEvent");
		ResultScanner scanner = frequencyAllEvent.getScanner(new Scan());
		int nbDate = classification.getNumberOfDayForTheOverallPeriod();
		ArrayList<Double> allFreqAll = new ArrayList<Double>();
		for (Result res = scanner.next(); res != null; res = scanner.next()) {
			String row = Bytes.toString(res.getRow());	
			System.out.println(row);
			if(row.contains(" ")) continue;
			double proportion =(res.size()/(double)nbDate) * 100;
			if(proportion > 0.6) continue;
			ArrayList<Double> arrFreq = new ArrayList<Double>();
			for(KeyValue kValue: res.list()){
				double currentFrequency = Double.parseDouble(Bytes.toString(kValue.getValue()));
				arrFreq.add(currentFrequency);
			}
			allFreqAll.add(Calculations.calculateMean(arrFreq));
		}
		double meanForAllLessThanNPercent = Calculations.calculateMean(allFreqAll);
		System.out.println(meanForAllLessThanNPercent);
		System.out.println(Calculations.roundTwoDecimals(meanForAllLessThanNPercent));
		System.out.println(nbDate);
		System.out.println(allFreqAll.size());
		frequencyAllEvent.close();
		System.exit(-1);
	}
	
	public static void main(String[]a){
		try {
			/**
			 * Results: mean = 0.0014915399997799878
			 * mean = 8.906493923690638E-4
			 */
			train();
		} catch (Exception e) {
			System.err.println(e);
		}
	}
}
